package com.fhnew.app.widget;


import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.fhnew.app.R;

public class LoadingDialog {

    private ViewGroup rootLayout;//Activity 的根视图
    private ViewGroup container;//Dialog 的视图

    private ImageView lodinView;
    private View pressView;

    private LinearLayout tipsLayout;
    private Button b_Cancel,b_Continue;

    private static final int OUTSIDE_POSITION = 1000;
    private  Animation intoAnimation ,outAnimation;

    private LodingDialogListener lodingDialogListener;

    private long firstClick = 0;
    private static final int DOUBLE_CLICK_TIME = 1000;

    private boolean isShow = false;
    private boolean cancelable = true;
    private boolean showTips = false;


    public LoadingDialog (ViewGroup rootLayout){
        this.rootLayout = rootLayout;

//        rootLayout = (ViewGroup) activity.findViewById(android.R.id.content).getRootView();

        container = (ViewGroup) LayoutInflater.from(rootLayout.getContext()).inflate(R.layout.fragment_dialog,null);

        ViewGroup.LayoutParams lp = container.getLayoutParams();
        if(lp == null){
            lp = new ViewGroup.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT);
        }else{
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        }
        container.setLayoutParams(lp);

        lodinView = container.findViewById(R.id.imageView_LodingDialog);

        pressView = container.findViewById(R.id.view_LodingDialog_PressView);
        pressView.setOnClickListener(onClickListener);

        tipsLayout = container.findViewById(R.id.linearLayout_LodingDialog_Tips);
        b_Cancel = container.findViewById(R.id.button_LodingDialog_Cancel);
        b_Cancel.setOnClickListener(onClickListener);
        b_Continue = container.findViewById(R.id.button_LodingDialog_Continue);
        b_Continue.setOnClickListener(onClickListener);
    }

    /**
     * 在指定根
     */
    public void showLoading(){
        if(isShow)return;
        isShow = true;

        Animation loadingAnimator = new RotateAnimation(0,3600,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        loadingAnimator.setDuration(10000);
        loadingAnimator.setRepeatCount(1000);
        lodinView.startAnimation(loadingAnimator);

        pressView.setVisibility(View.VISIBLE);

        rootLayout.addView(container);
    }

    public void dismiss(){
        if(!isShow)return;
        isShow = false;

        pressView.setVisibility(View.GONE);
        tipsLayout.setVisibility(View.GONE);

        rootLayout.removeView(container);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick (View v) {
            switch (v.getId()){
                case R.id.view_LodingDialog_PressView:
                    if(showTips){
                        if(System.currentTimeMillis() - firstClick > DOUBLE_CLICK_TIME){
                            firstClick = System.currentTimeMillis();
                        }else{
                            tipsInto();
                            pressView.setVisibility(View.GONE);
                        }
                    }else if(cancelable){
                        dismiss();
                    }
                    break;
                case R.id.button_LodingDialog_Cancel:
                    if(lodingDialogListener!= null)
                        lodingDialogListener.cancelAction();
                    dismiss();
                    break;
                case R.id.button_LodingDialog_Continue:
                    tipsOut();
                    break;
            }
        }
    };

    private Animation.AnimationListener outAnimationListener = new Animation.AnimationListener() {
        @Override
        public void onAnimationStart (Animation animation) {

        }

        @Override
        public void onAnimationEnd (Animation animation) {
            pressView.setVisibility(View.VISIBLE);
            tipsLayout.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationRepeat (Animation animation) {

        }
    };


    private void tipsInto(){
        if(intoAnimation == null){
            intoAnimation = new TranslateAnimation(0,0,-OUTSIDE_POSITION,0);
            intoAnimation.setDuration(550);
        }
        tipsLayout.startAnimation(intoAnimation);
        tipsLayout.setVisibility(View.VISIBLE);
    }

    private void tipsOut(){
        if(outAnimation == null){
            outAnimation = new TranslateAnimation(0,0,0,OUTSIDE_POSITION);
            outAnimation.setDuration(550);
            outAnimation.setAnimationListener(outAnimationListener);
        }
        tipsLayout.startAnimation(outAnimation);
    }


    public boolean isShow () {
        return isShow;
    }

//    public void setShow (boolean show) {
//        isShow = show;
//    }

    public void setDialogCancelable(boolean cancelable){
        this.cancelable= cancelable;
    }

    public void setShowTips(boolean showTips){
        this.showTips = showTips;
    }

    public void setLodingDialogListener(LodingDialogListener lodingDialogListener){
        this.lodingDialogListener = lodingDialogListener;
    }

    public interface LodingDialogListener{
        public void cancelAction ();
    }

}
